import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class User {
    private String username;
    private String password;
    private List<String> tickets;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
        this.tickets = new ArrayList<>();
    }

    public String getUsername() {
        return username;
    }

    public boolean authenticate(String password) {
        return this.password.equals(password);
    }

    public void buyTicket(String ticket) {
        tickets.add(ticket);
    }

    public List<String> getTickets() {
        return tickets;
    }
}

class TicketService {
    private List<User> users;
    private User loggedInUser;

    public TicketService() {
        users = new ArrayList<>();
        loggedInUser = null;
    }

    public void register(String username, String password) {
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                System.out.println("User already exists!");
                return;
            }
        }
        users.add(new User(username, password));
        System.out.println("User registered successfully!");
    }

    public void login(String username, String password) {
        for (User user : users) {
            if (user.getUsername().equals(username) && user.authenticate(password)) {
                loggedInUser = user;
                System.out.println("Login successful!");
                return;
            }
        }
        System.out.println("Invalid username or password!");
    }

    public void buyTicket(String ticket) {
        if (loggedInUser != null) {
            loggedInUser.buyTicket(ticket);
            System.out.println("Ticket purchased successfully!");
        } else {
            System.out.println("Please log in first!");
        }
    }

    public void viewTickets() {
        if (loggedInUser != null) {
            List<String> tickets = loggedInUser.getTickets();
            if (tickets.isEmpty()) {
                System.out.println("No tickets purchased.");
            } else {
                System.out.println("Purchased tickets:");
                for (String ticket : tickets) {
                    System.out.println(ticket);
                }
            }
        } else {
            System.out.println("Please log in first!");
        }
    }

    public void logout() {
        if (loggedInUser != null) {
            loggedInUser = null;
            System.out.println("Logged out successfully!");
        } else {
            System.out.println("No user is currently logged in.");
        }
    }
}

public class TicketSystem {
    public static void main(String[] args) {
        TicketService ticketService = new TicketService();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.println("3. Buy Ticket");
            System.out.println("4. View Tickets");
            System.out.println("5. Logout");
            System.out.println("6. Exit");
            System.out.print("Choose an option: ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter username: ");
                    String registerUsername = scanner.nextLine();
                    System.out.print("Enter password: ");
                    String registerPassword = scanner.nextLine();
                    ticketService.register(registerUsername, registerPassword);
                    break;
                case 2:
                    System.out.print("Enter username: ");
                    String loginUsername = scanner.nextLine();
                    System.out.print("Enter password: ");
                    String loginPassword = scanner.nextLine();
                    ticketService.login(loginUsername, loginPassword);
                    break;
                case 3:
                    System.out.print("Enter ticket details: ");
                    String ticket = scanner.nextLine();
                    ticketService.buyTicket(ticket);
                    break;
                case 4:
                    ticketService.viewTickets();
                    break;
                case 5:
                    ticketService.logout();
                    break;
                case 6:
                    System.out.println("Exiting...");
                    scanner.close();
                    return;
                default:
                    System.out.println("Invalid choice! Please try again.");
            }
        }
    }
}